package slim3.service;

import java.util.List;
import java.util.Map;

import slim3.meta.PostMeta;
import slim3.model.Post;
import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;

public class PostService {
    
    private PostMeta b = PostMeta.get();
    
    public void insert(Post post) {
        Transaction tx = Datastore.beginTransaction();
        Datastore.put(tx, post);
        Datastore.commit(tx);
    }
    
    public Post update(Key key, Long version, Map<String, Object> input) {
        Transaction tx = Datastore.beginTransaction();
        Post post = Datastore.get(tx, b, key, version);
        BeanUtil.copy(input, post);
        Datastore.put(tx, post);
        Datastore.commit(tx);
        return post;
    }
    
    public Post get(Key key, Long version) {
        return Datastore.get(b, key, version);
    }

    public List<Post> getAll() {
        return Datastore.query(b).asList();
    }
    
}
